<?php
/**
 * Created by PhpStorm.
 * User: silenceper
 * Date: 16/9/9
 * Time: 上午9:53
 */


namespace mybackend\controllers;


use common\services\budget\BudgetService;
use common\entities\myhtsq\ActionCode;

class BudgetController extends BaseController {
    private $_budgetService;

    public function __construct($id, $module, BudgetService $budgetService, $config = []) {
        $this->_budgetService = $budgetService;
        parent::__construct($id, $module, $config);
    }

    protected function roleAuthRules() {
        return [
            'actions' => [ 'List', 'Save'],
            'rules' => [
                [
                    'allow' => true,
                    'actionCodeMaps' => [
                        'Save' => ActionCode::BUDGET_MANAGE_MOD,
                        'List' => ActionCode::BUDGET_MANAGE_VIEW,
                    ]
                ]
            ]
        ];
    }

    public function actionList() {
        $data = $this->_budgetService->getList();
        $this->exportJson($data, $this->_budgetService->getErrCode(), $this->_budgetService->getErrMsg());
    }

    public function actionSave() {
        $this->exitIfNotPost();
        $year = \Yii::$app->request->request("year");
        $inputData = \Yii::$app->request->request("detail");
        $data = json_decode($inputData, true);

        if (empty($year)) {
            $this->exportJson([], "-1", "参数必须");
        }

        $res = $this->_budgetService->save($year, $data);
        $this->exportJson($res, $this->_budgetService->getErrCode(), $this->_budgetService->getErrMsg());
    }
}